package cc.opcol.iot.app.web;


import cc.opcol.iot.app.util.MongodbUtil;
import cc.opcol.iot.app.vo.GeneralQueryCondition;
import cc.opcol.iot.server.repo.MongoEventHandleRepository;
import cc.opcol.iot.server.sdk.JSInterface;
import cc.opcol.iot.server.util.GsonUtil;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/api/data")
public class DataController {
    Gson gson = new Gson();

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    JSInterface jsInterface;


    @RequestMapping(value = "/q", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String query(GeneralQueryCondition condition) {
        BasicQuery query = MongodbUtil.newQuery(condition);

        if (!StringUtils.isBlank(condition.getOrder())) {
            Document document = Document.parse(condition.getOrder());
            query.setSortObject(document);
        }else {
            Document document = Document.parse("{\"_id\":-1}");
            query.setSortObject(document);
        }
        query.skip(condition.getStartCount()).limit(condition.getLimit());
        List<Map> dataMap = mongoTemplate.find(query, Map.class, condition.getNamespace());
        return gson.toJson(new ResourceVo(dataMap));
    }


    @RequestMapping(value = "/save",method = {RequestMethod.POST}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String save(@RequestBody String data,String namespace){
        Map map= GsonUtil.getGson().fromJson(data,Map.class);
        if(map.get("_id")==null || (Double)map.get("_id")<1){
            map.put("_id",new Date().getTime());
        }
        mongoTemplate.save(map,namespace);

        if(namespace.equals(MongoEventHandleRepository.NAMESPACE)){
            jsInterface.loadAllEventScript();
        }
        return gson.toJson(new ResourceVo(map));
    }

    @RequestMapping(value = "/del", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String del(String namespace,Long id){
        Query query=new Query(Criteria.where("_id").is(id.longValue()));
        long count = mongoTemplate.remove(query,Map.class,namespace).getDeletedCount();
        if(namespace.equals(MongoEventHandleRepository.NAMESPACE)){
            jsInterface.loadAllEventScript();
        }
        return gson.toJson(new ResourceVo(count));
    }
}
